package com.ys.nms.module.business.controller.admin.portsVlans;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.ys.nms.framework.common.pojo.PageParam;
import com.ys.nms.framework.common.pojo.PageResult;
import com.ys.nms.framework.common.pojo.CommonResult;
import com.ys.nms.framework.common.util.object.BeanUtils;
import static com.ys.nms.framework.common.pojo.CommonResult.success;

import com.ys.nms.framework.excel.core.util.ExcelUtils;

import com.ys.nms.framework.apilog.core.annotation.ApiAccessLog;
import static com.ys.nms.framework.apilog.core.enums.OperateTypeEnum.*;

import com.ys.nms.module.business.controller.admin.portsVlans.vo.*;
import com.ys.nms.module.business.dal.dataobject.portsVlans.PortsVlansDO;
import com.ys.nms.module.business.service.portsVlans.PortsVlansService;

@Tag(name = "管理后台 - ports_vlans")
@RestController
@RequestMapping("/business/ports-vlans")
@Validated
public class PortsVlansController {

    @Resource
    private PortsVlansService portsVlansService;

    @PostMapping("/create")
    @Operation(summary = "创建ports_vlans")
    @PreAuthorize("@ss.hasPermission('business:ports-vlans:create')")
    public CommonResult<Integer> createPortsVlans(@Valid @RequestBody PortsVlansSaveReqVO createReqVO) {
        return success(portsVlansService.createPortsVlans(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新ports_vlans")
    @PreAuthorize("@ss.hasPermission('business:ports-vlans:update')")
    public CommonResult<Boolean> updatePortsVlans(@Valid @RequestBody PortsVlansSaveReqVO updateReqVO) {
        portsVlansService.updatePortsVlans(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除ports_vlans")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:ports-vlans:delete')")
    public CommonResult<Boolean> deletePortsVlans(@RequestParam("id") Integer id) {
        portsVlansService.deletePortsVlans(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除ports_vlans")
                @PreAuthorize("@ss.hasPermission('business:ports-vlans:delete')")
    public CommonResult<Boolean> deletePortsVlansList(@RequestParam("ids") List<Integer> ids) {
        portsVlansService.deletePortsVlansListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得ports_vlans")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:ports-vlans:query')")
    public CommonResult<PortsVlansRespVO> getPortsVlans(@RequestParam("id") Integer id) {
        PortsVlansDO portsVlans = portsVlansService.getPortsVlans(id);
        return success(BeanUtils.toBean(portsVlans, PortsVlansRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得ports_vlans分页")
    @PreAuthorize("@ss.hasPermission('business:ports-vlans:query')")
    public CommonResult<PageResult<PortsVlansRespVO>> getPortsVlansPage(@Valid PortsVlansPageReqVO pageReqVO) {
        PageResult<PortsVlansDO> pageResult = portsVlansService.getPortsVlansPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PortsVlansRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出ports_vlans Excel")
    @PreAuthorize("@ss.hasPermission('business:ports-vlans:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportPortsVlansExcel(@Valid PortsVlansPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PortsVlansDO> list = portsVlansService.getPortsVlansPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "ports_vlans.xls", "数据", PortsVlansRespVO.class,
                        BeanUtils.toBean(list, PortsVlansRespVO.class));
    }

}